(function () {
    "use strict"; //JavaScript代码标志为“严格模式

    var personInChargeController=function ($scope, $http,$location,$modal,personInChargeService) {
        $scope.vm = {
            pageSize:10,
            pageNumber:1
        };
        $scope.tableData = {};
        $scope.errorMsgs = [];
        $scope.hasError = false;
        $scope.find = function () {
            personInChargeService.picFind().success(function (data) {
                if(data.errorMsg != null){
                    $.Huimodalalert(data.errorMsg,1000);
                } else {
                    $scope.tableData = data.result;
                    var oTable = new TableInit();
                    oTable.Init();
                }
            });
        };
        $scope.find();
        var $table = $('#etlVt'),
            $remove = $('#remove'),
            $add = $('#add'),
            $save = $('#save'),
            $refresh = $('#refresh'),
            selections = [];
        var TableInit = function () {
            var oTableInit = new Object();
            oTableInit.Init = function () {
                $('#etlVt').bootstrapTable({
                    data:$scope.tableData,
                    toolbar: '#toolbar',
                    striped: true,
                    cache: false,
                    showRefresh :false,
                    pagination: true,
                    sortable: false,
                    sortOrder: 'asc',
                    search:true,
                    sidePagination: "client",
                    queryParams: oTableInit.queryParams,
                    pageNumber: 1,
                    pageSize: 10,
                    clickToSelect: false,
                    uniqueId: true,
                    cardView: false,
                    detailView: false,
                    responseHandler : responseHandler,
                    rowStyle:rowStyle,
                    rowAttributes:rowAttributes,
                    columns: [
                        {
                            field: 'state',
                            checkbox: true,
                            align: 'center',
                            valign: 'middle'
                        },{
                            field: 'picName',
                            title: '名称',
                            editable: true,
                            align: 'center',
                            valign: 'middle'
                        }, {
                            field: 'role',
                            title: '职位',
                            editable: {
                                type: 'select',
                                title: '职位',
                                source:[
                                    {value:"PM",text:"部署经理"},
                                    {value:"DM",text:"项目经理"},
                                    {value:"FORMAL_DEV",text:"研发负责人"},
                                    {value:"FORMAL_TEST",text:"测试负责人"},
                                    {value:"ASSISTANT_DEV",text:"研发负责人(外协)"},
                                    {value:"ASSISTANT_TEST",text:"测试负责人(外协)"}
                                    ]
                            },
                            align: 'center',
                            valign: 'middle'
                        }, {
                            field: 'picPhone',
                            title: '联系方式',
                            editable: true,
                            align: 'center',
                            valign: 'middle'
                        }]
                });
            };
            return oTableInit;
        };
        function getIdSelections() {
            return $.map($table.bootstrapTable('getSelections'), function (row) {
                return row
            });
        }
        function responseHandler(res) {
            $.each(res.data, function (i, row) {
                row.state = $.inArray(row.id, selections) !== -1;
            });
            return res;
        }
        function rowStyle(row, index) {
            if(index % 2 == 0){
                return {
                    //classes: ''
                    classes: 'text-nowrap another-class',
                    css: {"border-top":"0px solid #ddd"}
                };
            } else {
                return {
                    //classes: 'info'
                    classes: 'text-nowrap another-class',
                    css: {"border-top":"0px solid #ddd"}
                }
            }
        }
        function rowAttributes(row, index) {
            if(index % 2 == 0){
                return {
                    'style': 'color:#6B6B6B;font-size:13px;background-color:white'
                };
            } else {
                return {
                    'style': 'color:#6B6B6B;font-size:13px;background-color:#F7FCFE'
                }
            }
        }

        $table.on('check.bs.table uncheck.bs.table ' +
            'check-all.bs.table uncheck-all.bs.table', function () {
            $remove.prop('disabled', !$table.bootstrapTable('getSelections').length);
            // save your data, here just save the current page
            selections = getIdSelections();
            // push or splice the selections if you want to save all data selections
        });
        $remove.click(function () {
            var rows = getIdSelections();
            var list = [];
            var deleteList = [];
            angular.forEach(rows,function (row) {
                if(row.id == null){
                    list.push(true);
                } else {
                    var item = {id:row.id};
                    deleteList.push(item);
                }
            });
            if(list.length > 0){
                $table.bootstrapTable('remove', {field: 'state', values: list});
                $.Huimodalalert("删除成功",1000);
            }
            if(deleteList.length > 0){
                personInChargeService.picBatchDelete(deleteList).success(function (data) {
                    if(data.errorMsg != null){
                        $.Huimodalalert(data.errorMsg,1000);
                    } else {
                        $.Huimodalalert("删除成功",1000);
                        $table.bootstrapTable('destroy');
                        $scope.find();
                    }
                });
            }
        });
        $refresh.click(function () {
            $table.bootstrapTable('destroy');
            $scope.init = false;
            $scope.find();
        });
        $save.click(function () {
            $scope.hasError = false;
            $scope.errorMsgs = [];
            var tableData  = $table.bootstrapTable('getData');
            for(var i = 0; i<tableData.length; i++){
                var row = tableData[i];
                if(row.picName == null || $.trim(row.picName) == ""){
                     $scope.errorMsgs.push("第"+i+"条数据，名称不能为空");
                     $scope.hasError = true;
                }
            }
            if($scope.hasError){
                if ($scope.errorMsgs.length > 5){
                    $scope.errorMsgs = $scope.errorMsgs.splice(0,5);
                }
                $scope.$apply();
            }
            if(!$scope.hasError){
                personInChargeService.picBatchSave(tableData).success(function (data) {
                    if(data.errorMsg != null){
                        $.Huimodalalert(data.errorMsg,1000);
                        //alert(data.errorMsg);
                    } else {
                        $.Huimodalalert("保存成功",1000);
                        $table.bootstrapTable('destroy');
                        $scope.init = false;
                        $scope.find();
                    }
                });
            }
        });
        $add.click(function () {
            var data = initNewRow();
            $table.bootstrapTable('insertRow', {index: 0, row: data});
        });
        function initNewRow() {
            var newRow = {
                state:false,
                picName:"",
                role:null,
                picPhone:""
            };
            return newRow;
        }
    };
    angular.module("cmss.setting",[])
        .controller("PersonInChargeController",["$scope","$http","$location","$modal","PersonInChargeService",personInChargeController])
})();
